<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Fans extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Fans_model');
    }
	
	public function index()
	{              
            //获取通知列表的数据
            $userid = $this->thatUser['id'];
            $this->load->model('Usernum_model');
            $this->load->model('activity_model');
            $this->load->model('User_model');
            $this->load->model('Userdetail_model');
            $oUsernum = $this->Usernum_model->get_by_id($userid, 'fansnum,concernnum,be_ordernum');
            
            // 获取登陆人的信息
            $userInfo = $this->User_model->get_by_id($userid);
            
            $arrParam = array();
            
            if ((3 != $userInfo['usertype']) && (2 != $userInfo['usertype'])){
                
                $this->load->model('Fansrecommend_model');
                $pagesize = 8;
                //                 $cField = '*';
                $cField = '*';
                //                 $arrWhere = array('usertype'=>1,'status<>'=>-1,'userlevel'=>0,'nickname<>'=>'');
                $arrWhere = array('display'=>1,'status'=>1,'nickname<>'=>'');
                $actList = $this->Fansrecommend_model->get_act_list($arrWhere, $cField, $pagesize);
                if ($actList)
                {
                    foreach($actList as $key=>$row) {
                        $actList[$key]['id'] = $row['outerid'];
                        $actList[$key]['w_time'] = $row['begtime']&&is_numeric($row['begtime']) ? (date('Y-m-d', (int)$row['begtime'])) : ''; 
                        $actList[$key]['endtime'] = date('Y-m-d', $row['inendtime']);
                        $actList[$key]['title'] = mb_substr($row['title'], 0,24);
                    }
                }
                
                
                
//                 $page = _get_page();
//                 // 通告列表
//                 $pagesize = 8;
//                 $arrWhere = array('display'=>'1','status'=>'1','inendtime>'=>time());//条件
//                 $cField = '*';

//                 $list = $this->activity_model->fetch_page($page, $pagesize, $arrWhere, $cField);
//                 // 处理时间
//                 if (!empty($list['rows'])) {
//                     foreach($list['rows'] as $key=>$row) {
//                         $list['rows'][$key]['w_time'] = $row['begtime']&&is_numeric($row['begtime']) ? (date('Y-m-d', (int)$row['begtime'])) : ''; 
//                         $list['rows'][$key]['endtime'] = date('Y-m-d', $row['inendtime']);
//                         $list['rows'][$key]['title'] = mb_substr($row['title'], 0,24);
//                     }
//                 }
                
                 //分页
                $pagecfg = array();
                $pagecfg['base_url']     = _create_url('m/Fans', $arrParam);
                $pagecfg['total_rows']   = 0;
                $pagecfg['cur_page'] = 0;
                $pagecfg['per_page'] = $pagesize;
                //$this->load->library('pagination');
                $this->pagination->initialize($pagecfg);
                $list['pages'] = $this->pagination->create_links();
                $list['rows'] = $actList;
            } else {
                // 推荐的模特列表
                $this->load->model('Fansrecommend_model');
                $pagesize = 8;
//                 $cField = '*';
                $cField = 'user.id,nickname,userlogo,company,showimg,showimg2,city';
//                 $arrWhere = array('usertype'=>1,'status<>'=>-1,'userlevel'=>0,'nickname<>'=>'');
                $arrWhere = array('usertype'=>1,'status<>'=>-1,'nickname<>'=>'','fans_kind'=>1);
				$list = $this->Fansrecommend_model->get_user_list($arrWhere, $cField, $pagesize);
                if (!empty($list)) {
                    foreach($list as $key => $user) {
                        $info =  $this->Userdetail_model->get_info_by_userid($user['id']);
                        $numInfo = $this->Usernum_model->get_info_by_userid($user['id']);
                        $list[$key]['num_info'] = $numInfo;
//                         echo $this->Userdetail_model->db->last_query()."<br/>";
                        $list[$key]['detail_info'] = $info;
                    }
//                     die;
                }
            }
            
            $result = array(
                    'list' => $list, // 列表 
                    'oUsernum' => $oUsernum,
                    'arrParam' => $arrParam,
            		'oSysAct' => _get_config('activity'),
                    );

            $this->load->view('m/fans',$result);
	}
        
        public function fans() {
            //获取粉丝的数据
            $userid = $this->thatUser['id'];
            $this->load->model('Usernum_model');
            $oUsernum = $this->Usernum_model->get_by_id($userid, 'fansnum,concernnum,be_ordernum');

            $keyword = $this->input->get('keyword');
                
            $page     = _get_page();
            $pagesize = 9;
            $arrParam = array();
            $arrWhere = array('fansuserid'=>$userid,'status >'=>0);		//条件
            $cField = 'userid as idolid,nickname,userlogo, fansuserid as my_id,type';
            if($keyword)
            {
                $arrParam['keyword'] = $keyword;
                $arrWhere['nickname like'] = "'%$keyword%'";
            }
                
            // 粉丝页面
            $fansFlag = $this->input->get('havefans');
            if ($fansFlag) {
                $arrParam = array('havefans'=>(int)$this->input->get('havefans'));
                $arrWhere = array('userid'=>$userid,'status >'=>0);
                $cField = 'userid as my_id,fansuserid as idolid,fansnickname ,fanslogo,type';

                if($keyword) {
                    $arrParam['keyword'] = $keyword;
                    $arrWhere['fansnickname like'] = "'%$keyword%'";
                }
            }

		

		$list = $this->Fans_model->fetch_page($page, $pagesize, $arrWhere, $cField);
		//echo $this->db->last_query();die;
		

		//分页
		$pagecfg = array();
		$pagecfg['base_url']     = _create_url('m/Fans/fans', $arrParam);
		$pagecfg['total_rows']   = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		//$this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();
                
                // 更新list中的新的个人中心
                $this->load->model('User_model');
                if (!empty($list['rows'])) {
                    foreach($list['rows'] as $key => $user) {
                        $userInfo = $this->User_model->get_info_by_id($user['idolid'], 'id,nickname as name,userlogo');
                        $list['rows'][$key]['user_info'] = $userInfo; 
                        $list['rows'][$key]['care_flag'] = FALSE; // 关注标志   
                        
                        $where = array(
                            'userid' => $user['idolid'],
                            'fansuserid' => $user['my_id'],
                        );
                            
                        $careInfo = $this->Fans_model->get_by_where($where);
                        
                        if (!empty($careInfo)) {
                            // 没有关注
                            $list['rows'][$key]['care_flag'] = TRUE;
                        }
                    }
                }
                

		$result = array(
			'list' => $list,
			'oUsernum' => $oUsernum,
			'arrParam' => $arrParam,
                        
			);
		
		$this->load->view('m/fans_act',$result);
        }
        
        

}